package com.yanqu.road.dao.impl.servercenter;


import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.servercenter.ChannelStatus;

import java.sql.*;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class SystemChannelStatusDaoImpl extends TempDataObjectDao<ChannelStatus> {

    public static String getChannelStatusSql = "select * from `t_s_system_channel_status` where `systemId`=? and `channelId`=?";
    public ChannelStatus getChannelStatus(int systemId,int channelId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getChannelStatusSql);
            int i = 1;
            call.setInt(i++, systemId);
            call.setInt(i++, channelId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateChannelStatusSql = "update `t_s_system_channel_status` set status=?,beginTime=?,endTime=? where systemId=? and channelId=?";
    @Override
    protected boolean updateData(ChannelStatus channelStatus) {
        channelStatus.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getServerDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, channelStatus.getStatus()));
        para.put(i++, new DbParameter(Types.TIMESTAMP, new Timestamp(channelStatus.getBeginTime().getTime())));
        para.put(i++, new DbParameter(Types.TIMESTAMP, new Timestamp(channelStatus.getEndTime().getTime())));
        para.put(i++, new DbParameter(Types.INTEGER, channelStatus.getSystemId()));
        para.put(i++, new DbParameter(Types.INTEGER, channelStatus.getChannelId()));
        if (getDb().executeNoneQuery(updateChannelStatusSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertChannelStatusSql = "insert into `t_s_system_channel_status`(systemId,channelId,status,beginTime,endTime) values (?,?,?,?,?)";
    @Override
    protected boolean addData(ChannelStatus channelStatus) {
        channelStatus.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getServerDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, channelStatus.getSystemId()));
        para.put(i++, new DbParameter(Types.INTEGER, channelStatus.getChannelId()));
        para.put(i++, new DbParameter(Types.INTEGER, channelStatus.getStatus()));
        para.put(i++, new DbParameter(Types.TIMESTAMP, new Timestamp(channelStatus.getBeginTime().getTime())));
        para.put(i++, new DbParameter(Types.TIMESTAMP, new Timestamp(channelStatus.getEndTime().getTime())));
        if (getDb().executeNoneQuery(insertChannelStatusSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected ChannelStatus parseData(ResultSet rs) throws SQLException {
        ChannelStatus channelStatus = new ChannelStatus();
        channelStatus.setSystemId(rs.getInt("systemId"));
        channelStatus.setChannelId(rs.getInt("channelId"));
        channelStatus.setStatus(rs.getInt("status"));
        channelStatus.setBeginTime(rs.getTimestamp("beginTime"));
        channelStatus.setEndTime(rs.getTimestamp("endTime"));

        return channelStatus;
    }

    public Map<Integer, ChannelStatus> getChannelStatusMap(int channelId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, ChannelStatus> channelStatusMap = new ConcurrentHashMap<>();
        String selectSql = "select * from `t_s_system_channel_status` where `channelId`=?";
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(selectSql);
            call.setLong(1, channelId);
            rs = call.executeQuery();
            while (rs.next()) {
                ChannelStatus channelStatus = parse(rs);
                channelStatusMap.put(channelStatus.getSystemId(), channelStatus);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return channelStatusMap;


    }
}